<?php

$id = empty($this->Param['id']) ? 0 : $this->Param['id'];
$this->Assign('id', $id);

$this->LoadHelper('RoleHelper');
$RoleHelper = new RoleHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('PurviewListHelper');
$PurviewListHelper = new PurviewListHelper();

if (empty($this->Param['option'])) {
    $data = $RoleHelper->roleRow(array('`r_id` = ?' => $id, '`r_status` = ?' => 'ok'));
    $data['r_role'] = unserialize($data['r_role']);
    if(!empty($data['r_package'])){
        $data['r_package'] = explode(',', $data['r_package']);
    }
    $this->Assign('data', $data);
    
    echo $this->GetView('edit.php');
} else {
    switch ($this->Param['option']) {
        case 'save':
            $this->Param['r_role'] = serialize($this->Param['r_role']);
            $this->Param['r_package'] = empty($this->Param['r_package']) ? '' : implode(',', $this->Param['r_package']);
            if($id > 0){
                $result = $RoleHelper->roleSave($this->Param, array('`r_id` = ?' => $id, '`r_status` = ?' => 'ok'));
            }else{
                $result = $RoleHelper->roleSave($this->Param);
            }
            if($result['success']){
                $desc = $id > 0 ? '修改角色信息' : '新增角色信息';
                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        case 'remove':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $result = $RoleHelper->roleRemove(array('`r_id` = ?' => $id, '`r_status` = ?' => 'ok'));
            if($result){
                $MemberHelper->saveLog($this->UserConfig, '删除角色', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('删除成功');
            }
            ErrorMsg::Debug('删除失败');
            break;
        case 'packedit':
            $data = $RoleHelper->packRow(array('`p_id` = ?' => $id, '`p_status` = ?' => 'ok'));
            $data['p_role'] = unserialize($data['p_role']);
            $this->Assign('data', $data);
            
            $purview = $PurviewListHelper->GetAll();
            $this->Assign('purview', $purview);
            
            echo $this->GetView('pack_edit.php');
            break;
        case 'packsave':
            $this->Param['p_role'] = serialize($this->Param['p_role']);
            if($id > 0){
                $result = $RoleHelper->packSave($this->Param, array('`p_id` = ?' => $id, '`p_status` = ?' => 'ok'));
            }else{
                $result = $RoleHelper->packSave($this->Param);
            }
            if($result['success']){
                $desc = $id > 0 ? '修改权限包信息' : '新增权限包信息';
                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        case 'packremove':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $result = $RoleHelper->packRemove(array('`p_id` = ?' => $id, '`p_status` = ?' => 'ok'));
            if($result){
                $MemberHelper->saveLog($this->UserConfig, '删除权限包', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('删除成功');
            }
            ErrorMsg::Debug('删除失败');
            break;
        default:
    }
    
}